package se.topshelf.golftracker.web.controller.continent;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import se.topshelf.golftracker.core.domain.Continent;
import se.topshelf.golftracker.core.domain.Country;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.web.validator.BaseEntityValidator;

/**
 *
 */
public class ContinentValidator extends BaseEntityValidator<Continent> {
    private BaseService baseService;

    public ContinentValidator(BaseService baseService) {
        super(Continent.class);
        this.baseService = baseService;
    }

    @Override
    protected void validateFields(Continent entityToCheck, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "code", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", REQUIRED_ERROR_CODE, REQUIRED_ERROR_DEFAULT_MESSAGE);
    }

    @Override
    protected BaseService getBaseService() {
        return baseService;
    }

    @Override
    protected boolean isNaturalKeyEqual(Continent savedEntity, Continent entityToCheck) {
        return savedEntity.getName().equals(entityToCheck.getName());
    }

    @Override
    protected void doRejectOfNaturalField(Errors errors, Continent entityToCheck) {
        errors.rejectValue("name", "duplicate", "Duplicate continent name for name " + entityToCheck.getName());
    }
}
